$(document).ready(function() {
	


	/*
	$('#projects a').mouseenter(function(){
		$('img',this).stop().animate({'margin-top':-50},200);
	}).mouseleave(function(){
		$('img',this).stop().animate({'margin-top':0},400);
	});
	*/

});
$(window).on('resize', function(){
	if (($('body').height()) < $(window).height()) {
		$('footer').addClass('fixed');
	} else {
		$('footer').removeClass('fixed');
	}
});
var template = 'projects';
$(window).load(function() {
	
	if( $('.timeline').hasClass('tech') ) {
			$('.timeline').timeline({
					categories:['влекачи','двуосови','триосови','четириосови','многоосови'],			
					nuberOfSegments:[10,10,10,10,10,10],
					yearsOn:false
			});
			
			template = 'tech';
	}else{
		$('.timeline').timeline({});
		if( $('.timeline').hasClass('news') ) template = 'news';
	}
		
	// Mouse Weel
	var intOverallDelta = 0;
	var lastAnimation = 0;
	var quietPeriod = 500;
		
	$('#slider').mousewheel(function(objEvent, intDelta){
		
		objEvent.preventDefault();
		var timeNow = new Date().getTime();
		
		if (timeNow > lastAnimation) {
			if (intDelta > 0){
				$('.timeline').timeline('right');
			}
			else if (intDelta < 0){
				$('.timeline').timeline('left');
			}
			
			lastAnimation = timeNow + quietPeriod;
		}
				
	});
	
	
		
	/*
	$('.item').mouseenter(function(){
		
		
		if($('.item_open[data-id="'+$(this).attr('data-id')+'"]').width() > 400 && $(this).hasClass('active') ){
			$('.zoom',this).fadeIn();
		}
		
		
	}).mouseleave(function(){
		
		if($('.item_open[data-id="'+$('.item.active').attr('data-id')+'"]').width() > 400 && $(this).hasClass('active')){
			$('.zoom',this).stop().fadeOut();
		}
		
	});
	*/
	
	$('.timeline_open_images a').click(function(e){
		e.preventDefault();
	
		if($(this).hasClass('video'))
		{
			
			var video = $(this).attr('data-title');
		
			$('#popup').html('<iframe style="margin:2% 5%;" width="90%" height="90%" src="//www.youtube.com/embed/'+video+'?rel=0" frameborder="0" allowfullscreen=""></iframe>');
			$('#popup').fadeIn(300,function(){
				
				
			});
			
		}else{
		var image = $(this).attr('href');
		
		$('#popup').html('<img style="display:none;" src="'+image+'">');
		$('#popup').fadeIn(300,function(){
			
			var widht = ($(window).width() - $('#popup img').width())/2;
			var height = ($(window).height() - $('#popup img').height())/2;
			$('#popup img').css( 'margin-left', widht+'px');
			$('#popup img').css( 'margin-top', height+'px');
			$('#popup img').fadeIn(300);
			
		});
		}
							
	});	
	
	$('#popup').click(function(){
		$(this).fadeOut();
		$(this).html('');
	});	
	
	
	
});	

	var page = 2;

function loaditem(id)
{
	if(template == 'projects'){
	
				$('.timeline').timeline('destroy');
				$(".loadLoader").show();
				 $.ajax({
						type: 'POST',
						url: 'http://ff.studiokipo.com/ajax.php',
						data: 'start='+page,
						async: false,
						success: function(msg){
							
							$(".timeline").html(msg);
							$('.timeline').timeline({
								startItem:id});
							
							$(".loadLoader").delay(500).fadeOut(200);
							page++;
							
							
							
							
						},
						error: function(msg){
							alert('Err:'+ msg)
						}
					});
	}
					
}

jQuery.fn.extend({
	mousewheel: function(up, down, preventDefault) {
		return this.hover(
			function() {
				jQuery.event.mousewheel.giveFocus(this, up, down, preventDefault);
			},
			function() {
				jQuery.event.mousewheel.removeFocus(this);
			}
		);
	},
	mousewheeldown: function(fn, preventDefault) {
		return this.mousewheel(function(){}, fn, preventDefault);
	},
	mousewheelup: function(fn, preventDefault) {
		return this.mousewheel(fn, function(){}, preventDefault);
	},
	unmousewheel: function() {
		return this.each(function() {
			jQuery(this).unmouseover().unmouseout();
			jQuery.event.mousewheel.removeFocus(this);
		});
	},
	unmousewheeldown: jQuery.fn.unmousewheel,
	unmousewheelup: jQuery.fn.unmousewheel
});


jQuery.event.mousewheel = {
	giveFocus: function(el, up, down, preventDefault) {
		if (el._handleMousewheel) jQuery(el).unmousewheel();
		
		if (preventDefault == window.undefined && down && down.constructor != Function) {
			preventDefault = down;
			down = null;
		}
		
		el._handleMousewheel = function(event) {
			if (!event) event = window.event;
			if (preventDefault)
				if (event.preventDefault) event.preventDefault();
				else event.returnValue = false;
			var delta = 0;
			if (event.wheelDelta) {
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			} else if (event.detail) {
				delta = -event.detail/3;
			}
			if (up && (delta > 0 || !down))
				up.apply(el, [event, delta]);
			else if (down && delta < 0)
				down.apply(el, [event, delta]);
		};
		
		if (window.addEventListener)
			window.addEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = el._handleMousewheel;
	},
	
	removeFocus: function(el) {
		if (!el._handleMousewheel) return;
		
		if (window.removeEventListener)
			window.removeEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = null;
		el._handleMousewheel = null;
	}
};
